﻿namespace SLClient.ViewModels
{
    using SLClient.CustomerControls;
    using SLClient.Extension;
    using SLClient.Infrastructure;
    using SLClient.ServiceAPI;
    using SLClient.Views;
    using System;
    using System.Collections.ObjectModel;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Input;
    using System.ComponentModel;

    public class MainpageViewModel : ViewModelBase
    {
        private ViwUserPlayInfo _viwUserPlayInfo;

        public MainpageViewModel()
        {
            this.CommandInit();
            NewPlayInfos = new ObservableCollection<ViwUserPlayInfo>();
            NewOrders();
        }

        public void CommandInit()
        {
            RelayCommand command = new RelayCommand(this.OpenUserBank) {
                IsEnabled = true
            };
            this.ShowUserBankCommnad = command;
            RelayCommand command2 = new RelayCommand(this.OpenMoneyExport) {
                IsEnabled = true
            };
            this.ShowMoneyExportCommand = command2;
            RelayCommand command3 = new RelayCommand(this.Exit) {
                IsEnabled = true
            };
            this.ExitCommand = command3;
        }

        public void Exit()
        {
            LoginOut();
        }

        public static void LoginOut()
        {
            ServiceClient instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            instance.LoginOutCompleted += delegate (object obj, AsyncCompletedEventArgs arg) {
                (Application.Current.RootVisual as MainFrame).Initlogin();
            };
            instance.LoginOutAsync(ViewDispatcher.LoginUser.UserId);
        }

        public static void NewOrders()
        {
            ServiceClient instance = WCFServicesUtil<ServiceClient>.GetInstance(ServiceType.ServiceAPI);
            instance.GetNewUserPlayInfosCompleted += delegate (object obj, GetNewUserPlayInfosCompletedEventArgs arg) {
                NewPlayInfos.Clear();
                foreach (ViwUserPlayInfo info in arg.Result)
                {
                    NewPlayInfos.Add(info);
                }
            };
            instance.GetNewUserPlayInfosAsync(ViewDispatcher.LoginUser.UserId);
        }

        public void OpenMoneyExport()
        {
            new MoneyExportView().Show();
        }

        public void OpenUserBank()
        {
            new UserBankView().Show();
        }

        public void ShowUserPlayInfo()
        {
            new OrderWindow(this.SelectedViwUserPlayInfo).Show();
        }

        public ICommand ExitCommand { get; set; }

        public UserInfo LoginUser
        {
            get
            {
                return ViewDispatcher.LoginUser;
            }
        }

        public static ObservableCollection<ViwUserPlayInfo> NewPlayInfos
        {
            get;
            set;
        }

        public ViwUserPlayInfo SelectedViwUserPlayInfo
        {
            get
            {
                return this._viwUserPlayInfo;
            }
            set
            {
                this._viwUserPlayInfo = value;
                base.Notify<ViwUserPlayInfo>(() => this.SelectedViwUserPlayInfo);
               // base.Notify<ViwUserPlayInfo>(System.Linq.Expressions.Expression.Lambda<Func<ViwUserPlayInfo>>(System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression.Constant(this, typeof(MainpageViewModel)), (MethodInfo)(MainpageViewModel.get_SelectedViwUserPlayInfo)), new ParameterExpression[0]));
            }
        }

        public ICommand ShowMoneyExportCommand { get; set; }

        public ICommand ShowUserBankCommnad { get; set; }
    }
}

